var v = new Vue({
    el:"#app",
    data:{
        times:[],
        time:"",
        movieid:"",
        money:0,
        moviename:"",
        tableData:{},
        allMoviesShow:{},
        dialogFormVisible:false,
        userMessage:{},
        formLabelWidth:'80px',
        movieMsg:{sell:0},
        showMovies:{},
        //排片
        userMsg:{userid:"",username:""},
        linksdata:{},
        checkboxGroup:[],
        selectedSeats:[],
        seats:[],
        stepIndex:2,
        selectPay:'1',
        jstime:3,
        alredySeat:[],
        indentid:"",
        myhtml:"",
    },
    created(){
        this.getUserMsg();
        this.queryMovieList();
        this.queryShowList();
        this.queryTimes();
        this.queryLinks();
    },
    computed:{
        countMoney(){
            return this.movieMsg.sell * this.selectedSeats.length;
        },
        isShow(){
            return this.selectedSeats.length===0;
        },
        showSeat(index=1){
            return (index=1)=>{
                return this.alredySeat.includes(index);
            }
        },
        turnTime(time=0){
            return (time=0)=>{
                 return new Date(time).toLocaleString().substr(10,5);
            }
        },
        showTime(time=0){
            return (time=0)=>{
                return time.substr(6,5);
            }
        },
        showTime2(time=0){
            return (time=0)=>{
                return time.substr(6,5).replace('-','月');
            }
        },
    },
    methods:{
        show(){
            this.selectedSeats=[];
            this.checkboxGroup.forEach((value)=>{
              //第几排
              var h=Math.ceil(value/5);
              //第几列
              var s = value%5===0?5:value%5;
              var seat = h+"排"+s+"座";
              this.selectedSeats.push(seat);
            });
        },
        //查询排片计划
        queryMovieList(){
            console.log('得到用户')
            axios.get("QuryShowListMoviesServlet.s").then(res=>{
                this.allMoviesShow=res.data;
                this.showMovies=this.allMoviesShow.data[0];
                this.movieid=this.showMovies.movieid;
            });
        },
        queryArrMovie(index){
            this.time="3月28日";
            this.showMovies=this.allMoviesShow.data[index];
            this.movieid=this.showMovies.movieid;
            this.queryShowList();
        },
        queryShowList(){
            axios.get("BuyShowMoviesServlet.s",{
                params:{
                    starttime:this.time,
                    movieid:this.movieid,
                }
            }).then(res=>{
                this.tableData = res.data;
            });
            this.$forceUpdate();
        },
        openMyDialog(){
            this.dialogFormVisible=true;
        },
        queryTimes(){
          axios.get("QueryMovieTimeServlet.s").then(res=>{
              this.times=res.data.data;
          });
        },
        buytickt(message){
            if (this.userMsg.username===""){
                this.$alert('您还未登录,请登录后在进行操作 <a href="userlogin.html" style="color: red;font-size: 16px">立即登录</a>', '提示', {
                    dangerouslyUseHTMLString: true
                });
                return
            }
            axios.get("QureySelectedSeatsServlet.s",{
                params:{
                  ppjhid:message['ppjhid']
                }
            }).then(res=>{
                this.alredySeat = res.data.data;
            });
            this.checkboxGroup=[];
            this.selectedSeats=[];
            this.$forceUpdate();
            for (let name in message) {
                this.movieMsg[name] = message[name];
            }
            let seats = message['seats'];
            this.seats=[];
            for (let i = 1; i <=seats ; i++) {
                //第几排
                var h=Math.ceil(i/5);
                //第几列
                var s = i%5===0?5:i%5;
                var seat = h+"排"+s+"座";
                this.seats.push(seat);
            }

            this.stepIndex=2;
            this.dialogFormVisible=true;
        },
        pay(){
            if (this.checkboxGroup.length===0){
                this.$message.error("请选择座位!");
                return;
            }
            this.stepIndex=3;
          //  保存订单
            axios.get("SaveBuyServlet.s",{
                params:{
                    userid:this.userMsg.userid,
                    ppjhid:this.movieMsg.ppjhid,
                    num:this.selectedSeats.length,
                    seats:this.selectedSeats+"",
                    money: this.countMoney,
                    balance:this.userMsg.money,
                },
            }).then(res=>{
                if (res.data.code===1){
                    this.$message({
                        message:res.data.msg,
                        type:'success',
                    });
                    this.indentid=res.data.data.indentid;
                }else{
                    this.$message.error(res.data.msg);
                }
            });
            this.$forceUpdate();
        },
        goBack(){
         if (this.stepIndex>2){
             this.stepIndex =this.stepIndex-1;
         }
        },
        jishiqi(){
           this.jstime=3;
            var a =setInterval(()=>{
                if (this.jstime<=0){
                    clearInterval(a);
                    this.dialogFormVisible=false;
                    return false;
                }
                this.jstime--;
            },1000);
        },
        //付款
        buy(){
            if (this.payway==='1'){
                axios.get("MoneyPayServlet.s",{
                    params:{
                        userid:this.userMsg.userid,
                        ppjhid:this.movieMsg.ppjhid,
                        num:this.selectedSeats.length,
                        money: this.countMoney,
                        balance:this.userMsg.money,
                        payway:this.selectPay,
                    },
                }).then(res=>{
                    if (res.data.code===1){
                        this.$message({
                            message:res.data.msg,
                            type:'success',
                        });
                        this.stepIndex=4;
                        this.queryShowList();
                        this.getUserMsg();
                        this.jishiqi();

                    }else{
                        this.$message.error(res.data.msg);
                    }
                });
                this.$forceUpdate();
            }else {
                axios.get("PayServlet.s",{
                    params:{
                        indentid:this.indentid,
                        userid:this.userMsg.userid,
                        ppjhid:this.movieMsg.ppjhid,
                        num:this.selectedSeats.length,
                        money: this.countMoney,
                        balance:this.userMsg.money,
                        payway:this.selectPay,
                    }
                }).then(res=>{
                    if (res.data==="成功"){
                        window.open("http://localhost:8080/YYDSCINEMA_war_exploded/AlipayServlet");
                    }

                });
                this.stepIndex=4;
                this.queryShowList();
                this.$forceUpdate();
            }

        },
        getUserMsg(){
            axios.get("GetUserMessageServlet.s").then(res=>{
                if (res.data.data!=null){
                    this.userMsg=res.data.data;
                }
            });
        },
        queryLinks(){
            console.log("获取用户")
            axios.get("UserQueryLinksServlet.s").then(res=>{
                console.log('赋值')
                this.linksdata=res.data;
            })
        },
    }

});